package meiyunting.shiwuzhaoling.controller;

import meiyunting.shiwuzhaoling.model.Claim;
import meiyunting.shiwuzhaoling.model.ClaimStatus;
import meiyunting.shiwuzhaoling.model.Item;
import meiyunting.shiwuzhaoling.model.ItemStatus;
import meiyunting.shiwuzhaoling.model.User;
import meiyunting.shiwuzhaoling.service.ClaimService;
import meiyunting.shiwuzhaoling.service.ItemService;
import meiyunting.shiwuzhaoling.service.UserService;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/profile")
public class UserProfileController {

    private final UserService userService;
    private final ItemService itemService;
    private final ClaimService claimService;

    public UserProfileController(UserService userService, ItemService itemService, ClaimService claimService) {
        this.userService = userService;
        this.itemService = itemService;
        this.claimService = claimService;
    }

    @GetMapping
    public String showUserProfile(Model model, RedirectAttributes redirectAttributes) {
        // 检查用户是否登录
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth == null || auth.getPrincipal().equals("anonymousUser")) {
            redirectAttributes.addFlashAttribute("errorMessage", "请先登录后再访问个人中心");
            return "redirect:/login";
        }
        
        User currentUser = userService.getCurrentUser();
        
        // 获取用户发布的物品
        List<Item> publishedItems = itemService.getItemsByUser(currentUser);
        
        // 获取用户认领的物品（通过认领记录）
        List<Claim> userClaims = claimService.getClaimsByUser(currentUser);
        
        // 获取已批准的认领
        List<Claim> approvedClaims = userClaims.stream()
                .filter(claim -> claim.getStatus() == ClaimStatus.APPROVED)
                .collect(Collectors.toList());
        
        // 获取待处理的认领
        List<Claim> pendingClaims = userClaims.stream()
                .filter(claim -> claim.getStatus() == ClaimStatus.PENDING)
                .collect(Collectors.toList());
        
        // 获取已拒绝的认领
        List<Claim> rejectedClaims = userClaims.stream()
                .filter(claim -> claim.getStatus() == ClaimStatus.REJECTED)
                .collect(Collectors.toList());
        
        // 获取已完成的物品（状态为COMPLETED）
        List<Item> completedItems = publishedItems.stream()
                .filter(item -> item.getStatus() == ItemStatus.COMPLETED)
                .collect(Collectors.toList());
        
        model.addAttribute("user", currentUser);
        model.addAttribute("publishedItems", publishedItems);
        model.addAttribute("approvedClaims", approvedClaims);
        model.addAttribute("pendingClaims", pendingClaims);
        model.addAttribute("rejectedClaims", rejectedClaims);
        model.addAttribute("completedItems", completedItems);
        
        return "user/profile";
    }
} 